package com.cn.service;

import com.cn.dao.RoleDao;
import com.cn.dao.UserDao;
import com.cn.domain.Role;
import com.cn.domain.UserDetail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

/**
 * Created by chenyuncong on 2019/9/12.
 */
@Component
public class CustomUserDetailsService implements UserDetailsService {

    @Autowired
    private UserDao userDao;
    @Autowired
    private RoleDao roleDao;

    @Override
    public UserDetails loadUserByUsername(String account) throws UsernameNotFoundException {
        //数据库查询用户信息
        UserDetail ud=null;
        try {
            ud = userDao.getUserDetail(account);
            if(ud==null){
                throw new UsernameNotFoundException("没有 "+account+" 这个账号");
            }
            Role role=roleDao.getRole(ud.getId());
            ud.setRole(role);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return ud;
    }
}
